package controller

import (
	"strconv"
	"time"
	"zzxq/common/codes"
	"zzxq/common/middleware/response"
	"zzxq/service"

	"github.com/gin-gonic/gin"
)

type Remind struct{}

//待办集-获取 date 时间戳
func (r *Remind) GetDateList(c *gin.Context) {
	result := response.NewResponse(c)
	uid := c.GetInt("uid")

	date := c.Query("date")
	datetime, _ := strconv.ParseInt(date, 10, 64)

	if datetime == 0 || datetime < 0 {
		result.Resp(3000, result.WithMsg("参数异常"))
		return
	}

	timepbj := time.Unix(datetime, 0)

	serobj := &service.Remind{}
	remindlist := serobj.GetDateList(uid, timepbj)

	var data = make(map[string]interface{})

	var finish int = 0
	var total int = len(*remindlist)

	for _, value := range *remindlist {
		if value.Status == 1 {
			finish++
		}
	}

	data["total"] = total
	data["list"] = remindlist

	result.Resp(codes.SUCCESS, result.WithData(data))

}

func (r *Remind) AddRemind(c *gin.Context) {
	uid := c.GetInt("uid")

	type Request struct {
		Date  int64  `form:"date" binding:"required,gt=0"`
		Title string `form:"title" binding:"required,min=5,max=30"`
	}

	var request Request
	if err := c.ShouldBind(&request); err != nil {
		response.RespF(c, 3001, "参数异常")
		return
	}

	serobj := &service.Remind{}
	ret := serobj.AddRemind(uid, request.Title, time.Unix(request.Date, 0))

	if !ret {
		response.RespF(c, 3003, "系统异常")
		return
	}

	response.RespS(c, codes.SUCCESS)
}
